﻿package net.wangit.dev.component;

import net.wangit.MapList;
import net.wangit.Path;
import net.wangit.framework.config.Domains;
import net.wangit.support.MapListFactory;
import net.wangit.util.FileUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class AlterableDomainsMapListFactory implements MapListFactory {

	private static final Log log = LogFactory
			.getLog(AlterableDomainsMapListFactory.class);

	public AlterableDomainsMapListFactory() {
	}

	public MapList getMapList() {
		try {
			MapList list;
			list = new MapList();
			int index = 0;
			String[] domains = FileUtil
					.listDirectorys(Path.getDomainRootPath());
			for (int i = 0; i < domains.length; i++) {
				String domain = domains[i];
				String datasourcePath = (new StringBuilder(Path
						.getDomainPath(domain))).append("datasource.xml")
						.toString();
				if (!Domains.isSystemDomain(domain)
						&& FileUtil.exists(datasourcePath)) {
					list.put(index, "value", domain);
					list.put(index, "name", domain);
					index++;
				}
			}
			return list;
		} catch (Exception e) {
			log.error("create applicable domains enumeration error", e);
			return null;
		}
	}
}
